#!/usr/local/bin/pythonw
# -*- coding: utf-8 -*-
import re
import sys
#from string import *
import tool_dimeco as td
import os

sys.path.append('./rstr_max/')
from tools_karkkainen_sanders import *
from rstr_max import *
from optparse import OptionParser

def get_content(texte):
    contenu = open(texte,'r')
    chaine = contenu.read()
    contenu.close()
    pp = re.compile(re.escape('<p'))
    ps = re.compile(re.escape('.'))
    paras = [m.start() for m in pp.finditer(chaine)]
#    if len(paras)<5:
#      paras = [m.start() for m in ps.finditer(chaine)]
    zones = []
    c = 0
    l = len(paras)
    for p in paras:
      c+=1
      if c<len(paras):
        zones.append([p,paras[c]])
      else:
        zones.append([p,len(chaine)-1])
    l = len(zones)
    zones=  zones[:3]+zones[l-2:l]
    vide = [[0,0]]
    if l==4:
      zones=  zones[:2]+vide+zones[l-2:l]
    if l==3:
      zones=  zones[:1]+2*vide+zones[l-2:l]
    if l==2:
      zones=  zones[:1]+3*vide+zones[l-1:l]
    return chaine,zones

def get_repet(textes):
  rstr = Rstr_max()
  set_zones = set()
  set_mala = set()
  liste_ss = []
  liste_idstr_textes=[]
  liste_id_par = {}
  cpt = 0
  num_texte = -1
  for texte in textes:
      su1 = unicode(texte,'utf-8')#.lower()
      rstr.add_str(su1)
  r = rstr.go()
  a = []
  for (offset_end, nb), (l, start_plage) in r.iteritems():
    l_pos = {}#Liste des pos d'une chaine répétée
    ss = rstr.global_suffix[offset_end-l:offset_end]
    if len(ss)>8:
      b = (len(ss),ss)
      a.append(b)
  for i,j in sorted(a,reverse=True)[:101]:
    print i,j



def analyser(liste_fichiers, options, pp, ps,liste_pert):
  relevantcontent, positions = extr_relevantcontent(liste_fichiers)
  cpt=-1
  out = ''
  l_pos =[0,1,2]
  l_pert=[]
  for r in relevantcontent:
    cpt+=1
    pot_pert = False
    s = {0:[],1:[]}
    for texte,pos in positions[cpt].iteritems():
      pert = liste_pert[texte]
      int_pos = set(l_pos)
      set_pos = set(pos)
      if pert==1: 
        pot_pert = True
      if len(set_pos.intersection(int_pos))>=2:
        s[pert].append(pos)
    if pot_pert==True and len(s[1])>1:
      prob = float(len(s[1]))/(len(s[1])+len(s[0]))
      l_pert.append([prob,r,s])
  for p,string,pos in sorted(l_pert,reverse=True)[:1000]:
    print p,string
    print '--',pos
    
  b/0
  for i,s in l_pert:
    print i
    for l,t in s.iteritems():
      print '--',l
      print '----',t

def decoder(path_annotation, list_lg) :
  f = open(path_annotation,'r')
  s = f.read()
  d = eval(s)
  f.close()
  d_res = {}
  for lg in list_lg :
    d_res[lg] = {}
  for id_doc, info in d.iteritems() :
    dinfo = info
    lg = dinfo['langue']
#    if dinfo['annotations'] == [] : 
#      continue
    if 'multi' in list_lg:
      d_res['multi'][id_doc] = dinfo 
    if lg not in list_lg :
      continue
    d_res[lg][id_doc] = dinfo
  return d_res



if __name__ == "__main__":
  t1 = sys.argv[1]
  c1 = open(t1,'r').read()
  t2 = sys.argv[2]
  c2 = open(t2,'r').read()
  c1 = re.sub('\n|-\n','',c1)
  c2 = re.sub('\n|-\n','',c2)
  get_repet([c1,c2])
